package com.yc.favorite.servlet;

import com.yc.favorite.entity.User;
import com.yc.favorite.mapper.TagMapper;
import com.yc.favorite.util.MyBatisHelper;
import com.yc.favorite.util.ServletUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/queryTag")
public class QueryTagServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try(SqlSession sqlSession = MyBatisHelper.openSession();){
            TagMapper tagMapper = sqlSession.getMapper(TagMapper.class);
            ServletUtils.result("查询分类成功!", resp, ()->{
                User user = (User) req.getSession().getAttribute("myinfo");
                if(user == null){
                    return null;
                }
                return tagMapper.selectAll(user.getId());
            });
        }
    }
}
